//
//  WFEmoticonCVCell.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/20.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

class WFEmoticonCVCell: UICollectionViewCell {
    
    
    // MARK:  - 懒加载控件
    //定义数组,用来存储20个表情按钮
     private lazy var emoticonButtons: [WFEmoticonBtn] = [WFEmoticonBtn]()
    //定义删除按钮
    private lazy var deleteBtn:UIButton = {
        let btn = UIButton()
        //添加点击事件
        btn.addTarget(self, action: "deleteEmoticonButtonAction", forControlEvents: .TouchUpInside)
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return btn
    }()
    
    
    // MARK:  - 绑定emoticon模型
    var emoticons:[WFEmoticon]?{
        
        didSet{
            
            guard let ets = emoticons else {
                return
            }
            
            //隐藏全部按钮
            for value in emoticonButtons {
                value.hidden = true
            }
            
            for (i, value) in ets.enumerate() {
                let btn = emoticonButtons[i]
                //为按钮设置表情模型
                btn.emoticon = value
                btn.hidden = false
                
                if value .type == "0" {
                    btn.setImage(UIImage(named: value.path!), forState: .Normal)
                    btn.setTitle(nil, forState: .Normal)
                } else {
                    btn.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    btn.setImage(nil, forState: .Normal)
                }
                
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  - 初始化控件
    private func setUpUI(){
        addChildBtn()
    }
    
    // MARK:  - 循环添加按钮
    private func addChildBtn(){
        for _ in 0..<20 {
        let button = WFEmoticonBtn()
//            button.backgroundColor = RandomColor()
        button.titleLabel?.font = UIFont.systemFontOfSize(34)
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: .TouchUpInside)
        contentView.addSubview(button)
        emoticonButtons.append(button)

        }
        contentView.addSubview(deleteBtn)
        
        
    }
    // MARK:  - 设置删除按钮的点击事件
    @objc private func deleteEmoticonButtonAction(){
        //由于层级太深,也要通过通知来发送
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelecteDeleteEmoticonButtonNotification, object: nil)
    }
    
    // MARK:  - 设置emotion按钮的点击事件
    @objc private func emoticonButtonAction(btn:WFEmoticonBtn){
    
        let emotion = btn.emoticon
        //发送点击表情按钮的通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmotionButtonNotification, object:emotion)
    }
    
    // MARK:  - 计算每个btn 的坐标
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemW = width / 7
        let itemH = height / 3
        
        for (i, button) in emoticonButtons.enumerate() {
            let colIndex = i % 7
            let rowIndex = i / 7
            
             button.size = CGSize(width: itemW, height: itemH)
            button.x = CGFloat(colIndex) * itemW
            //  设置y坐标
            button.y = CGFloat(rowIndex) * itemH
        }
        
        deleteBtn.size = CGSizeMake(itemW, itemH)
        deleteBtn.x = width - itemW
        deleteBtn.y = height - itemH
    }
}
